package com.dushibao.service.impl;

import com.dushibao.common.Constants;
import com.dushibao.dao.GoodsClassDao;
import com.dushibao.entity.GoodsClass;
import com.dushibao.service.GoodsClassService;
import com.dushibao.utils.Page;
import com.dushibao.utils.SecurityHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 商品分类 实现类
 *
 * @author zqd
 * @date 2022-08-04 23:26:16
 */
public class GoodsClassServiceImpl implements GoodsClassService {
    GoodsClassDao goodsClassDao = new GoodsClassDao();

    @Override
    public List<GoodsClass> getParents() {
        return goodsClassDao.getParents();
    }

    @Override
    public Integer save(GoodsClass goodsClass) {
        return goodsClassDao.save(goodsClass);
    }

    @Override
    public Page page() {
        HttpServletRequest req = SecurityHolder.request.get();
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");

        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");

        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        req.setAttribute("startTime", startTime);
        req.setAttribute("endTime", endTime);

        Page page = new Page();
        page.setCurrent(Integer.parseInt(current == null ? "1" : current));
        page.setPageSize(Integer.parseInt(pageSize == null ? "10" : pageSize));

        return goodsClassDao.page(page, params);
    }

    @Override
    public GoodsClass getById(long id) {
        return goodsClassDao.getById(id);
    }

    @Override
    public Integer saveOrUpdate(GoodsClass goodsClass) {
        if (goodsClass.getId() != null) {
            return goodsClassDao.update(goodsClass);
        }
        return goodsClassDao.save(goodsClass);
    }

    @Override
    public Integer deleteById(List<Long> ids) {
        return goodsClassDao.deleteById(ids);
    }

    @Override
    public GoodsClass getByPid(long pid) {
        return goodsClassDao.getByPid(pid);
    }
}
